package com.binfenhulian.chinaenergy.ces.utils.hex;

import java.io.ByteArrayOutputStream;


public class HexUtil {
	public static String str2HexStr(String str) {
		char[] chars = "0123456789ABCDEF".toCharArray();
		StringBuilder sb = new StringBuilder("");
		byte[] bs = str.getBytes();

		for (int i = 0; i < bs.length; i++) {
			int bit = (bs[i] & 0x0f0) >> 4;
			sb.append(chars[bit]);
			bit = bs[i] & 0x0f;
			sb.append(chars[bit]);
		}
		return sb.toString();
	}

	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];

		for (int i = 0; i < bytes.length; i++) {
			int n = str.indexOf(hexs[(2 * i)]) * 16;
			n += str.indexOf(hexs[(2 * i + 1)]);
			bytes[i] = ((byte) (n & 0xFF));
		}
		return new String(bytes);
	}

	public static String bytesToHexString(byte[] bytes) {
		String result = "";
		for (int i = 0; i < bytes.length; i++) {
			String hexString = Integer.toHexString(bytes[i] & 0xFF);
			if (hexString.length() == 1) {
				hexString = '0' + hexString;
			}
			result = result + hexString.toUpperCase();
		}
		return result;
	}

	private static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
		byte ret = (byte) (_b0 | _b1);
		return ret;
	}

	public static byte[] hexString2Bytes(String src) {
		int len = src.length() / 2;
		byte[] ret = new byte[len];
		byte[] tmp = src.getBytes();
		for (int i = 0; i < len; i++) {
			ret[i] = uniteBytes(tmp[(i * 2)], tmp[(i * 2 + 1)]);
		}
		return ret;
	}

	public static String StringToAsciiString(String content) {
		String result = "";
		int max = content.length();
		for (int i = 0; i < max; i++) {
			char c = content.charAt(i);
			String b = Integer.toHexString(c);
			result = result + b;
		}
		return result;
	}

	public static String AsciiStringToString(String content) {
		String result = "";
		int length = content.length() / 2;
		for (int i = 0; i < length; i++) {
			String c = content.substring(i * 2, i * 2 + 2);
			int a = hexStringToAlgorism(c);
			char b = (char) a;
			String d = String.valueOf(b);
			result = result + d;
		}
		return result;
	}

	public static int hexStringToAlgorism(String hex) {
		hex = hex.toUpperCase();
		int max = hex.length();
		int result = 0;
		for (int i = max; i > 0; i--) {
			char c = hex.charAt(i - 1);
			int algorism = 0;
			if ((c >= '0') && (c <= '9'))
				algorism = c - '0';
			else {
				algorism = c - '7';
			}
			result = (int) (result + Math.pow(16.0D, max - i) * algorism);
		}
		return result;
	}

	public static String getInfoBuff(byte[] buff, int count) {
		byte[] temp = new byte[count];
		for (int i = 0; i < count; i++) {
			temp[i] = buff[i];
		}
		return new String(temp);
	}

	public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
		byte[] byte_3 = new byte[byte_1.length + byte_2.length];
		System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
		System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
		return byte_3;
	}

	public static byte[] byteMerger2(byte[] b1, byte[] b2) {
		return byteMerger(b1, b2);
	}

	public static byte[] byteMerger3(byte[] b1, byte[] b2, byte[] b3) {
		b1 = byteMerger(b1, b2);
		return byteMerger(b1, b3);
	}

	public static byte[] byteMerger4(byte[] b1, byte[] b2, byte[] b3, byte[] b4) {
		b1 = byteMerger(b1, b2);
		b1 = byteMerger(b1, b3);
		return byteMerger(b1, b4);
	}

	public static byte[] byteMerger5(byte[] b1, byte[] b2, byte[] b3, byte[] b4, byte[] b5) {
		b1 = byteMerger(b1, b2);
		b1 = byteMerger(b1, b3);
		b1 = byteMerger(b1, b4);
		return byteMerger(b1, b5);
	}

	public static byte[] byteMerger6(byte[] b1, byte[] b2, byte[] b3, byte[] b4, byte[] b5, byte[] b6) {
		b1 = byteMerger(b1, b2);
		b1 = byteMerger(b1, b3);
		b1 = byteMerger(b1, b4);
		b1 = byteMerger(b1, b5);
		return byteMerger(b1, b6);
	}

	public static byte[] byteMerger7(byte[] b1, byte[] b2, byte[] b3, byte[] b4, byte[] b5, byte[] b6, byte[] b7) {
		b1 = byteMerger(b1, b2);
		b1 = byteMerger(b1, b3);
		b1 = byteMerger(b1, b4);
		b1 = byteMerger(b1, b5);
		b1 = byteMerger(b1, b6);
		return byteMerger(b1, b7);
	}

	public static byte[] lenarr(String len) {
		String lenHex = Integer.toHexString(Integer.parseInt(len)).toUpperCase();

		if (lenHex.length() == 1)
			lenHex = "000" + lenHex;
		else if (lenHex.length() == 2)
			lenHex = "00" + lenHex;
		else if (lenHex.length() == 3) {
			lenHex = "0" + lenHex;
		}

		byte l11 = (byte) Integer.valueOf(lenHex.substring(0, 2), 16).intValue();
		byte l12 = (byte) Integer.valueOf(lenHex.substring(2, 4), 16).intValue();

		return new byte[] { l11, l12 };
	}

	public static byte[] lenarr2(String len) {
		String lenHex = Integer.toHexString(Integer.parseInt(len)).toUpperCase();

		if (lenHex.length() == 1) {
			lenHex = "0" + lenHex;
		}

		byte l11 = (byte) Integer.valueOf(lenHex.substring(0, 2), 16).intValue();

		return new byte[] { l11 };
	}

	public static byte[] str2cbcd(String s) {
		if (s.length() % 2 != 0) {
			s = "0" + s;
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		char[] cs = s.toCharArray();
		for (int i = 0; i < cs.length; i += 2) {
			int high = cs[i] - '0';
			int low = cs[(i + 1)] - '0';
			baos.write(high << 4 | low);
		}
		return baos.toByteArray();
	}

	public static String cbcd2str(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			int h = ((b[i] & 0xFF) >> 4) + 48;
			sb.append((char) h);
			int l = (b[i] & 0xF) + 48;
			sb.append((char) l);
		}
		return sb.toString();
	}

	public static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if ((ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
				|| (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS)
				|| (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
				|| (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION)
				|| (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION)
				|| (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)) {
			return true;
		}
		return false;
	}
	
	public static String hexString2binaryString(String hexString)  
    {  
        if (hexString == null || hexString.length() % 2 != 0)  
            return null;  
        String bString = "", tmp;  
        for (int i = 0; i < hexString.length(); i++)  
        {  
            tmp = "0000"  
                    + Integer.toBinaryString(Integer.parseInt(hexString  
                            .substring(i, i + 1), 16));  
            bString += tmp.substring(tmp.length() - 4);  
        }  
        return bString;  
    }

	public static void main(String[] args) {
		// String goodUrl = "http://vm.ksepton.com/ksepton/汉字.jpg";
		// StringBuffer url_unicode = new StringBuffer();
		// for (int i = 0; i < goodUrl.length(); i++) {
		// char c = goodUrl.charAt(i);
		// if (isChinese(c))
		// url_unicode.append(Integer.toHexString(c));
		// else {
		// url_unicode.append("00" + Integer.toHexString(c));
		// }
		// }
		//
		// String goodUrlHex = url_unicode.toString();
		// System.out.println(goodUrlHex);
//		String data = "01710022383632363331303337353334333031FF0001020000001107170A1C330D0A";
//		String data = "01710022383632363331303337353336343936FF0001020000001107180A22250D0A";
//		System.out.println(HexUtil.AsciiStringToString(data.substring(8, 38)));
//		System.out.println(data.substring(2, 4));
//		System.out.println(HexUtil.hexStringToAlgorism(data.substring(38, 40)));
//		String orderNo = data.substring(48, 52);
//		System.out.println("orderNo=="+orderNo);
//		int huodao = HexUtil.hexStringToAlgorism(data.substring(46, 48));
//		String shijian = data.substring(52, 64);
//		System.out.println(shijian);
//		
//		byte[] lenarr = HexUtil.lenarr("16");
//
//		byte[] param1 = { 1, 113, lenarr[0], lenarr[1], 1, 1 };
//		
//		System.out.println(HexUtil.bytesToHexString(param1));
//		param1 = HexUtil.byteMerger2(param1, sjarr(shijian));
//		byte[] param3 = { 0, 0 };
//		System.out.println(HexUtil.bytesToHexString(param3));
		
		
		
		

		String data = "01710022383636383733303232343732363238030001000106641005010102080D0A";
		System.out.println(data.substring(48, 52));

		
//		System.out.println("mtype==="+HexUtil.hexStringToAlgorism(data.substring(44, 46)));
//		System.out.println("status==="+HexUtil.hexStringToAlgorism(data.substring(40, 44)));
//		lenarr = HexUtil.lenarr("16");

//		param1 = { 1, 113, lenarr[0], lenarr[1], 1, 1 };
//		param1 = HexUtil.byteMerger2(param1, sjarr(shijian));
//		System.out.println(HexUtil.bytesToHexString((sjarr(shijian))));
//		System.out.println("xxxx==="+HexUtil.hexString2Bytes(HexUtil.bytesToHexString((param1))));
	}
	
	public static byte[] sjarr(String shijian) {
		byte b1 = (byte) Integer.valueOf(shijian.substring(0, 2), 16).intValue();
		byte b2 = (byte) Integer.valueOf(shijian.substring(2, 4), 16).intValue();
		byte b3 = (byte) Integer.valueOf(shijian.substring(4, 6), 16).intValue();
		byte b4 = (byte) Integer.valueOf(shijian.substring(6, 8), 16).intValue();
		byte b5 = (byte) Integer.valueOf(shijian.substring(8, 10), 16).intValue();
		byte b6 = (byte) Integer.valueOf(shijian.substring(10, 12), 16).intValue();
		byte[] sjarr = { b1, b2, b3, b4, b5, b6, 13, 10 };

		return sjarr;
	}
}